package com.prelearning.at_springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.prelearning.at_springboot.utils.ExcelUtil;
import com.prelearning.at_springboot.utils.WorkWXAPIUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.prelearning.at_springboot.service.IPublishedService;
import com.prelearning.at_springboot.entity.Published;

import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cgr
 * @since 2022-04-10
 */
@RestController
@RequestMapping("/published")
public class PublishedController {

    @Resource
    private IPublishedService publishedService;

    //定时器定时清洗数据库
    @Scheduled(cron = "*/5 * * * * ?")
    private void process() {
        publishedService.refresh();
    }

    // 新增或者更新
    @PostMapping
    public boolean save(@RequestBody Published published) {
        boolean save=publishedService.saveOrUpdate(publishedService.init(published));
        WorkWXAPIUtil.postMessage(published.getGroupId());
        return save;
    }

    //点击和投票签到
    @PostMapping("/sign/{id}/{choice}")
    public boolean Sign_online(@PathVariable String id,@PathVariable int choice,@RequestBody String studentInfo){
        return publishedService.Sign_online(id,choice,studentInfo);
    }

    //根据签到表id删除
    @DeleteMapping("/del/{id}")
    public Boolean delete(@PathVariable String id) {
            return publishedService.removeById(id);
    }

    //多项删除
    @PostMapping("/del/batch")
    public boolean deleteBatch(@RequestBody List<String> ids) {
            return publishedService.removeByIds(ids);
    }

    //根据群主id获取已完成签到
    @GetMapping("/all/{owner_id}")
    public List<Published> findPublished(@PathVariable String owner_id) { return publishedService.getPublished(owner_id); }

    //获取所有已发布签到
    @GetMapping
    public List<Published> findAll() {
            return publishedService.list(new QueryWrapper<Published>().orderByDesc("creat_time"));
    }

    //根据签到表的id获取签到
    @GetMapping("/{id}")
    public Published findOne(@PathVariable String id) {
            return publishedService.getById(id);
    }

    //根据用户id获取未完成签到
    @GetMapping("/unfinished/{user_id}")
    public List<Published> findUnfinished(@PathVariable String user_id) { return publishedService.getUnfinished(user_id); }

    //根据用户id获取已发布的签到
    @GetMapping("/finished/{user_id}")
    public List<Published> findFinished(@PathVariable String user_id) { return publishedService.getFinished(user_id); }

    //导出签到数据为excel
    @GetMapping("/downloadExcel/{id}")
    @ApiOperation(value = "下载Excel")
    public void getExcelData(HttpServletResponse response, @PathVariable String id) throws IOException {
        ExcelUtil.readExcelToIO (publishedService.getById(id), response);
    }


}

